#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：13_ThreadLocal对象在线程中使用.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/6/9/009 22:14 
"""

import threading

# 创建全局ThreadLocal对象:
local_school = threading.local()


def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello, %s (in %s)' % (std, threading.current_thread().name))


def process_thread(name):
    # 绑定ThreadLocal的student:
    local_school.student = name  # 两个线程传入不同的值，互不干扰，取值是直接使用 local_shool.student 就可以取出
    process_student()


t1 = threading.Thread(target=process_thread, args=('dongGe',), name='Thread-A')
t2 = threading.Thread(target=process_thread, args=('老王',), name='Thread-B')
t1.start()
t2.start()
